package org.lql.algo.codecrush.week008;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liangqinglong
 * @date: 2025-08-19 00:03
 * @description: 49. 字母异位词分组 <a href="https://leetcode.cn/problems/group-anagrams/description/">...</a>
 **/
public class GroupAnagrams {

	public List<List<String>> groupAnagrams(String[] strs) {
		Map<String, List<String>> groups = new HashMap<>();
		for (String str : strs) {
			char[] chars = str.toCharArray();
			Arrays.sort(chars);
			String key = new String(chars);
			groups.putIfAbsent(key, new ArrayList<>());
			groups.get(key).add(str);
		}
		return new ArrayList<>(groups.values());
	}

	public static void main(String[] args) {
		GroupAnagrams groupAnagrams = new GroupAnagrams();
		String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
		List<List<String>> lists = groupAnagrams.groupAnagrams(strs);
		System.out.println(lists);
	}
}
